package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.DTO.ArticleDTO;
import com.xing.DTO.UserDTO;
import com.xing.dao.ArticleDao;
import com.xing.entity.ArticleEntity;
import com.xing.service.ArticleService;
import com.xing.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/7/16 19:28
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {

    @Autowired
    private ArticleDao dao;

    public QueryWrapper<ArticleEntity> getArticle(Map<String, Object> params) {
        QueryWrapper<ArticleEntity> query = new QueryWrapper<ArticleEntity>();
        String uid = (String) params.get("uid");
        query.eq(StringUtils.isNoneBlank(uid), "user_id", uid);
        boolean orderBy = Boolean.parseBoolean((String) params.get("orderBy"));
        //升序
        query.orderByAsc(orderBy, "created");
        //降序
        query.orderByDesc(!orderBy, "created");
        return query;
    }


    @Override
    public Result Save(ArticleDTO dto) {
        ArticleEntity articleEntity = new ArticleEntity();
        articleEntity.setTitle(dto.getTitle());
        articleEntity.setContent(dto.getContent());
        articleEntity.setStatus(dto.getStatus());
        UserDTO principal = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        articleEntity.setUserId(principal.getUserId());
        articleEntity.setCreated(new Date());
        articleEntity.setLastModified(new Date());
        boolean save = save(articleEntity);
        if (!save) {
            return new Result().err(502, "文件保存异常");
        }
        return new Result().ok();
    }

    @Override
    public Result Page(Map<String, Object> params) {
        int page = Integer.parseInt((String) params.get("page"));
        int size = Integer.parseInt((String) params.get("size"));
        IPage<ArticleEntity> iPage = new Page<>(page, size);
        IPage<ArticleEntity> articleEntityIPage = dao.selectPage(iPage, getArticle(params));
        return new Result().ok(articleEntityIPage);
    }

    @Override
    public Result GetArticleById(Long id) {
        ArticleEntity byId = getById(id);
        return new Result().ok(byId);
    }

    @Override
    public Result PutArticleById(Long id, ArticleDTO dto) {
        UserDTO principal = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        ArticleEntity byId = getById(id);
        if (byId.getUserId().equals(principal.getUserId())) {
            byId.setTitle(dto.getTitle());
            byId.setTitle(dto.getContent());
            byId.setStatus(dto.getStatus());
            byId.setLastModified(new Date());
            saveOrUpdate(byId);
            return new Result().ok();
        }
        return new Result().err(406, "无法修改不属于自己的文章");
    }

    @Override
    public Result DeleteArticleById(Long id) {
        ArticleEntity byId = getById(id);
        UserDTO principal = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (byId.getUserId().equals(principal.getUserId())) {
            removeById(id);
            return new Result().ok();
        }
        return new Result().err(405, "无法删除不属于自己的文章");
    }


}
